﻿using BaseProject.Application.Interfaces;
using BaseProject.Domain.Configs;
using BaseProject.Domain.Enums;
using BaseProject.Infrastructure.Helpers;
using BaseProject.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace BaseProject.Web.Controllers
{
    [Authorize]
    public class UserController : BaseController
    {
        private readonly IOptions<AppSettingConfig> _settings;
        private readonly IUserService _userService;
        private readonly IAuthenticationService _authenticationService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IWorkContext _workContext;
        private ISession _session => _httpContextAccessor.HttpContext.Session;

        public UserController(IHttpContextAccessor httpContextAccessor, IUserService userService, IAuthenticationService authenticationService, IOptions<AppSettingConfig> settings, IWorkContext workContext)
        {
            this._httpContextAccessor = httpContextAccessor;
            this._userService = userService;
            this._authenticationService = authenticationService;
            this._settings = settings;
            this._workContext = workContext;
        }

        public IActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        public ActionResult CreateCode()
        {
            string code = CaptchaHelper.CreateValidateCode(4);
            HttpContext.Session.SetString("ValidateCode", code);
            byte[] bytes = CaptchaHelper.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

        [AllowAnonymous]
        public IActionResult Login()
        {
            var model = new LoginVM();
            return View(model);
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login(LoginVM model)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(model.ValidationCode) && Convert.ToString(HttpContext.Session.GetString("ValidateCode")) != model.ValidationCode)
                {
                    ModelState.AddModelError("", "验证码错误!");
                }
                else
                {
                    var loginResult = await _userService.Login(model.Mobile, model.Password);
                    switch (loginResult)
                    {
                        case UserLoginStatus.Successful:
                            {
                                var user = await _userService.GetUserByMobile(model.Mobile);
                                _authenticationService.SignIn(user);
                                return RedirectToAction("Index", "Home");
                            }
                        case UserLoginStatus.UserNotExist:
                            ModelState.AddModelError("", "用户不存在!");
                            break;
                        case UserLoginStatus.Deleted:
                            ModelState.AddModelError("", "用户已被删除!");
                            break;
                        case UserLoginStatus.Lock:
                            ModelState.AddModelError("", "用户已被禁用!");
                            break;
                        case UserLoginStatus.WrongPassword:
                            ModelState.AddModelError("", "手机号或密码错误!");
                            break;
                    }
                }
            }
            return View(model);
        }

        [AllowAnonymous]
        public ActionResult Logout()
        {
            _authenticationService.SignOut();
            return RedirectToAction("Login");
        }

        [AllowAnonymous]
        public ActionResult Test()
        {
            var str = UtilHelper.Md5("123456");
            return Content(str);
        }
    }
}